package com.hxexp.weixin.test;

import java.io.File;
import java.util.List;

import org.junit.Assert;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.MethodSorters;

import com.fasterxml.jackson.core.type.TypeReference;
import com.hxexp.weixin.common.json.JsonUtil;
import com.hxexp.weixin.model.CommonResult;
import com.hxexp.weixin.test.model.JsonUtilTestBean;

/**
 * @author li.jiang
 *
 */
@RunWith(BlockJUnit4ClassRunner.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public final class JsonUtilTester {
    @Test
    public void test01() {
        String json = "[{\"errcode\":0,\"errmsg\":\"hello world\"},{\"errcode\":1,\"errmsg\":\"hi\"}]";
        List<CommonResult> list = JsonUtil.readJsonString2Object(json, new TypeReference<List<CommonResult>>() {
        });
        System.out.println(list);
    }

    @Test
    public void test02() {
        JsonUtilTestBean jsonUtilTestBean = new JsonUtilTestBean();
        jsonUtilTestBean.setName("www");
        jsonUtilTestBean.setAge(39);
        String returnValue = JsonUtil.writeObject2JsonString(jsonUtilTestBean);
        System.out.println(returnValue);
        Assert.assertEquals("{\"name\":\"www\",\"age\":39}", returnValue);
    }

    @Test
    public void test03() {
        String rootPath = getClass().getResource("/").getFile();
        String path = rootPath + "test.txt";
        File file = new File(path);

        CommonResult returnValue = JsonUtil.readJsonFile2Object(file, new TypeReference<CommonResult>() {
        });
        System.out.println(returnValue);
        System.out.println(file.isFile());
        System.out.println(path);
    }

    @Test
    public void test04() {
        String json = "{\"errcode\":1,\"errmsg\":\"error\"}";
        CommonResult returnValue = JsonUtil.readJsonString2Object(json, CommonResult.class);
        System.out.println(returnValue);
    }
}
